/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package camada.apresentacao.dialog.servico;

import camada.bancodedados.daoimplemente.cliente.PessoaFisicaDAOImpl;
import camada.bancodedados.daoimplemente.servico.ServicoDAOImpl;
import combomodel.ComboBoxClienteModel;
import combomodel.servico.ComboBoxServicoCategoriaModel;
import eloma.classe.Cliente;
import eloma.classe.servico.Servico;
import eloma.classe.servico.ServicoCategoria;
import java.util.List;

/**
 *
 * @author GEMADA
 */
public class DialogCadServico extends javax.swing.JDialog {

    PessoaFisicaDAOImpl daoCliente = new PessoaFisicaDAOImpl();
    ServicoDAOImpl daoServico = new ServicoDAOImpl();

    String tipo;
    int idEmpresa;

    public DialogCadServico(java.awt.Frame parent, boolean modal, int idEmpresa) {
        super(parent, modal);
        initComponents();
        this.idEmpresa = idEmpresa;
        this.setLocationRelativeTo(null);
        carregarComboCliente();
        this.setTitle("Cadastro Serviço");
    }

    public void DialogCadServico(String tipo) {
        this.tipo = tipo;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtDataServico = new com.toedter.calendar.JDateChooser();
        jLabel2 = new javax.swing.JLabel();
        cmCliente = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescricao = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        rbAlta = new javax.swing.JRadioButton();
        rbMedia = new javax.swing.JRadioButton();
        rbBaixa = new javax.swing.JRadioButton();
        jLabel6 = new javax.swing.JLabel();
        cmCategoria = new javax.swing.JComboBox();
        btSalvar = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();

        jLabel3.setText("jLabel3");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados Serviço"));

        jLabel1.setText("Data do Serviço:");

        jLabel2.setText("Cliente:");

        cmCliente.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel4.setText("Descrição do Serviço:");

        txtDescricao.setColumns(20);
        txtDescricao.setRows(5);
        jScrollPane1.setViewportView(txtDescricao);

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/outros/Prioridade.png"))); // NOI18N
        jLabel5.setText("Prioridade:");

        buttonGroup1.add(rbAlta);
        rbAlta.setSelected(true);
        rbAlta.setText("Alta");

        buttonGroup1.add(rbMedia);
        rbMedia.setText("Média");
        rbMedia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbMediaActionPerformed(evt);
            }
        });

        buttonGroup1.add(rbBaixa);
        rbBaixa.setText("Baixa");

        jLabel6.setText("Categoria:");

        cmCategoria.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel1)
                        .addComponent(txtDataServico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2)
                        .addComponent(cmCliente, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel4)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE))
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(cmCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(rbAlta)
                        .addGap(29, 29, 29)
                        .addComponent(rbMedia)
                        .addGap(26, 26, 26)
                        .addComponent(rbBaixa)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtDataServico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbAlta)
                    .addComponent(rbMedia)
                    .addComponent(rbBaixa))
                .addContainerGap())
        );

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Save.png"))); // NOI18N
        btSalvar.setText("Salvar");
        btSalvar.setMaximumSize(new java.awt.Dimension(95, 25));
        btSalvar.setMinimumSize(new java.awt.Dimension(95, 25));
        btSalvar.setPreferredSize(new java.awt.Dimension(95, 25));
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        btCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Cancel.png"))); // NOI18N
        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btCancelar)
                .addGap(12, 12, 12))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        try {

            if (tipo == "Salvar") {

                Servico servico = new Servico();

                servico.setIdEmpresa(this.idEmpresa);

                ComboBoxClienteModel model = (ComboBoxClienteModel) cmCliente.getModel();
                Cliente cliente = model.getElementAt(cmCliente.getSelectedIndex());

                ComboBoxServicoCategoriaModel modelCategoria = (ComboBoxServicoCategoriaModel) cmCategoria.getModel();
                ServicoCategoria servicoCategoria = modelCategoria.getElementAt(cmCategoria.getSelectedIndex());

                servico.setServicoCategoria(servicoCategoria);
                servico.setDataServico(txtDataServico.getDate());
                servico.setCliente(cliente);
                servico.setDecricao(txtDescricao.getText());

                if (rbAlta.isSelected() == true) {
                    servico.setStatus(0);
                } else if (rbMedia.isSelected() == true) {
                    servico.setStatus(1);
                } else if (rbBaixa.isSelected() == true) {
                    servico.setStatus(2);
                }

                servico.setAvaliacao(null);

                daoServico.inserirServico(servico);

                this.hide();

            } else if (tipo == "Editar") {

                Servico servico = new Servico();

                ComboBoxClienteModel model = (ComboBoxClienteModel) cmCliente.getModel();
                Cliente cliente = (Cliente) model.getSelectedItem();

                servico.setDataServico(txtDataServico.getDate());
                servico.setCliente(cliente);
                servico.setDecricao(txtDescricao.getText());

                if (rbAlta.isSelected() == true) {
                    servico.setStatus(0);
                } else if (rbMedia.isSelected() == true) {
                    servico.setStatus(1);
                } else if (rbBaixa.isSelected() == true) {
                    servico.setStatus(2);
                }

            }

        } catch (Exception e) {
            String teste = e.getMessage();
        }
    }//GEN-LAST:event_btSalvarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btCancelarActionPerformed

    private void rbMediaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbMediaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rbMediaActionPerformed

    private void carregarComboCliente() {
        try {

            List<ServicoCategoria> listaCategoria = daoServico.selecionarServicoCategoria();
            List<Cliente> listaCliente = daoCliente.buscaTodos();

            ComboBoxServicoCategoriaModel modelCategoria = new ComboBoxServicoCategoriaModel(listaCategoria);
            cmCategoria.setModel(modelCategoria);
            ComboBoxClienteModel model = new ComboBoxClienteModel(listaCliente);
            cmCliente.setModel(model);

        } catch (Exception e) {

        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DialogCadServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DialogCadServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DialogCadServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DialogCadServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DialogCadServico dialog = new DialogCadServico(new javax.swing.JFrame(), true, 0);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btSalvar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cmCategoria;
    private javax.swing.JComboBox cmCliente;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton rbAlta;
    private javax.swing.JRadioButton rbBaixa;
    private javax.swing.JRadioButton rbMedia;
    private com.toedter.calendar.JDateChooser txtDataServico;
    private javax.swing.JTextArea txtDescricao;
    // End of variables declaration//GEN-END:variables

}
